package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.generator.dao.CodeDicDao;
import io.renren.modules.generator.dao.EnterpriseServiceArchiveDao;
import io.renren.modules.generator.entity.CodeDicEntity;
import io.renren.modules.generator.entity.EnterpriseServiceArchiveEntity;
import io.renren.modules.generator.service.EnterpriseServiceArchiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


@Service("enterpriseServiceArchiveService")
public class EnterpriseServiceArchiveServiceImpl extends ServiceImpl<EnterpriseServiceArchiveDao, EnterpriseServiceArchiveEntity> implements EnterpriseServiceArchiveService {

    //@Autowired
    //private EnterpriseServiceArchiveDao enterpriseServiceArchiveDao;

    @Autowired
    private CodeDicDao codeDicDao;

    //@Autowired
    //private EnterpriseMainDao enterpriseMainDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //List<String> inlist = new ArrayList<>();
        //if (enterpriseServiceArchiveEntity != null && StringUtils.isNotBlank(enterpriseServiceArchiveEntity.getDeptId())) {
        //    inlist = enterpriseMainDao.getList(enterpriseServiceArchiveEntity.getDeptId());
        //    if (!CollectionUtils.isEmpty(inlist)) {
        //        inlist.add(enterpriseServiceArchiveEntity.getDeptId());
        //    } else {
        //        inlist  = new ArrayList<>();
        //        inlist.add(enterpriseServiceArchiveEntity.getDeptId());
        //    }
        //}
        QueryWrapper queryWrapper = new QueryWrapper<>();
        //if (params.get("id") != null) {
        //    queryWrapper.eq("id", params.get("id"));
        //}
        if (params.get("enterprise_id") != null) {
            queryWrapper.eq("enterprise_id", params.get("enterprise_id"));
        }
        if (params.get("service_type_first") != null && StringUtils.isNotBlank(params.get("service_type_first").toString())) {
            queryWrapper.like("service_type_first", params.get("service_type_first").toString());
        }
        if (params.get("service_type_second") != null && StringUtils.isNotBlank(params.get("service_type_second").toString())) {
            queryWrapper.like("service_type_second", params.get("service_type_second").toString());
        }
        if (params.get("service_content") != null && StringUtils.isNotBlank(params.get("service_content").toString())) {
            queryWrapper.like("service_content", params.get("service_content").toString());
        }
        //if (!CollectionUtils.isEmpty(inlist)) {
        //    queryWrapper.in("b.dept_id", inlist);
        //}
        queryWrapper.orderByDesc("create_time", "update_time");
        IPage<EnterpriseServiceArchiveEntity> page = this.page(
                new Query<EnterpriseServiceArchiveEntity>().getPage(params),
                queryWrapper
        );
        QueryWrapper<CodeDicEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(CodeDicEntity::getType,"enterprise_service_archive");
        List<CodeDicEntity> codeDicEntities = codeDicDao.selectList(queryWrapper1);
        for (EnterpriseServiceArchiveEntity entity : page.getRecords()) {
            for (CodeDicEntity codeDicEntity: codeDicEntities) {
                if (StringUtils.equals(entity.getServiceTypeFirst(), String.valueOf(codeDicEntity.getId()))) {
                    entity.setServiceTypeFrstName(codeDicEntity.getName());
                }
                if (StringUtils.equals(entity.getServiceTypeSecond(), String.valueOf(codeDicEntity.getId()))) {
                    entity.setServiceTypeSecondName(codeDicEntity.getName());
                }
            }
        }
        return new PageUtils(page);
    }


    @Override
    public EnterpriseServiceArchiveEntity getById(Serializable id) {
        EnterpriseServiceArchiveEntity entity = super.getById(id);
        QueryWrapper<CodeDicEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CodeDicEntity::getType, "enterprise_service_archive");
        List<CodeDicEntity> codeDicEntities = codeDicDao.selectList(queryWrapper);
        for (CodeDicEntity codeDicEntity : codeDicEntities) {
            if (StringUtils.equals(entity.getServiceTypeFirst(), String.valueOf(codeDicEntity.getId()))) {
                entity.setServiceTypeFrstName(codeDicEntity.getName());
            }
            if (StringUtils.equals(entity.getServiceTypeSecond(), String.valueOf(codeDicEntity.getId()))) {
                entity.setServiceTypeSecondName(codeDicEntity.getName());
            }
        }
        return entity;
    }
}
